package com.zq.im.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zq.im.MessageActivity;
import com.zq.im.R;
import com.zq.im.activity.FriendRequestListActivity;
import com.zq.im.bean.FriendListBean;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by 张强 869518570@qq.com on 2020/8/26
 */
public class FriendListAdapter extends RecyclerView.Adapter<FriendListAdapter.FriendsItemViewHolder> {

    private int ITEM_TYPE_NORMAL = 0;
    private int ITEM_TYPE_HEADER = 1;
    private View mHeaderView;

    private View headerView;

    private List<FriendListBean> mFriendsBeans;
    private Context mContext;

    public FriendListAdapter(List<FriendListBean> myFriends, Context context) {
        this.mFriendsBeans = myFriends;
        mContext = context;
    }

    public void setFriendsBeans(List<FriendListBean> friendsBeans) {
        mFriendsBeans = friendsBeans;
    }

    public void addHeaderView(View headerView) {
        this.headerView = headerView;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0 && null != headerView) {
            return ITEM_TYPE_HEADER;
        }
        return ITEM_TYPE_NORMAL;
    }

    @NonNull
    @Override
    public FriendsItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE_HEADER) {
            return new FriendsItemViewHolder(headerView);
        }
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.friends_item, parent, false);
        return new FriendsItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull FriendsItemViewHolder holder, int position) {
        // get data ,set data
        if (position == 0) {
            Glide.with(mContext).load(R.mipmap.add_friends).into(holder.icon);
            holder.name.setText("新的朋友");
        } else if (position > 0) {
            FriendListBean bean = mFriendsBeans.get(position - 1);
            Glide.with(mContext).load(bean.getFriendFaceImage()).error(R.mipmap.icon).into(holder.icon);
            holder.name.setText(bean.getFriendUsername());
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (position == 0) {//打开添加好友页面
                    mContext.startActivity(new Intent(mContext, FriendRequestListActivity.class));
                } else { //打开聊天界面
                    Intent intent = new Intent(mContext, MessageActivity.class);
                    FriendListBean friendListBean = mFriendsBeans.get(position - 1);
                    intent.putExtra("receiverId", friendListBean.getFriendUserId());
                    intent.putExtra("nickName", friendListBean.getFriendNickname());
                    mContext.startActivity(intent);
                }
            }
        });


    }

    @Override
    public int getItemCount() {
        if (mFriendsBeans != null) {
            return mFriendsBeans.size() + 1;
        }
        return 1;
    }

    class FriendsItemViewHolder extends RecyclerView.ViewHolder {
        ImageView icon;
        TextView name;

        public FriendsItemViewHolder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.name);
            icon = itemView.findViewById(R.id.icon);
        }
    }

}
